<?php

include_once("../dao/conexao_class.php");
include_once("../model/predio_model_class.php");
include_once("../controller/campus_controller_class.php");


class PredioDAO {

    private $insert = "INSERT INTO predio(cam_id, pre_nome) values( ?, ?);";
    private $update = "UPDATE predio SET cam_id, pre_nome WHERE pre_id = ?;";
    private $delete = "DELETE FROM predio WHERE pre_id = ?;";
    private $select = "SELECT * FROM predio;";
    private $select_by_id = "SELECT * FROM predio WHERE pre_id = ?;";
    private $select_by_campus = "SELECT * FROM predio WHERE cam_id = ?;";
    private $select_by_predio_campus = "SELECT * FROM predio WHERE pre_nome = ? AND cam_id = ?;";

    function insert($objPredio) {
        //conectando ao banco
        $objConn = new Conexao();
        $conn = $objConn->conecta();
//        var_dump($conn);
        //criando o preparedStatment

        if ($stm = $conn->prepare($this->insert)) {

            $stm->bind_param("is", $objPredio->getCampus(), $objPredio->getNome());

            if ($stm->execute()) {
                $conn->close();
                return true;
            } else {
                $conn->close();
                return false;
            }
            
        } else {
            $conn->close();
            return false;
        }
    }

    function update($objPredio) {

        $objConn = new Conexao();
        $conn = $objConn->conecta();

        if ($stm = $conn->prepare($this->update)) {

            $stm->bind_param("isi", $objPredio->getCampus(), $objPredio->getNome(), $objPredio->getId());

            if ($stm->execute()) {
                $conn->close();
                return true;
            } else {
                $conn->close();
                return false;
            }

        }
        return false;
    }

    function delete($id) {

        $objConn = new Conexao();
        $conn = $objConn->conecta();

        if ($stm = $conn->prepare($this->delete)) {

            $stm->bind_param("i", $id);

            if ($stm->execute()) {
                $conn->close();
                return true;
            } else {
                $conn->close();
                return false;
            }
        }
        return false;
    }

    function findAllPredio() {

        $objConn = new Conexao();
        $conn = $objConn->conecta();
        $preM = new PredioModel();
        $lista = array();
        
        if ($stm = $conn->prepare($this->select)) {

            $stm->execute();

            //atribui o resultado encontrado as variaveis
            $stm->bind_result($id, $campus, $nome);


            //para cada resultado encontrado
            $i = 0;
            
            while ($stm->fetch()) {
                $preM = new PredioModel();
                
                $objCampus = new CampusController();
                
                $campus = $objCampus->selecionarCampusPorId($campus);
                
                
                $preM->setId($id);
                $preM->setCampus($campus);
                $preM->setNome($nome);
                
                $lista[$i++] = $preM;
                
            }

        }
        $conn->close();
        return $lista;
    }
    
    function findPredioById($predio) {

        $objConn = new Conexao();
        $conn = $objConn->conecta();
        $preM = new PredioModel();
        
        
        if ($stm = $conn->prepare($this->select_by_id)) {

            $stm->bind_param("i", $predio);

            $stm->execute();

            //atribui o resultado encontrado as variaveis
            $stm->bind_result($id, $camp, $nome);

            while ($stm->fetch()) {

                $preM = new PredioModel();

                $objCampus = new CampusController();

                $campus = $objCampus->selecionarCampusPorId($camp);

                $preM->setId($id);
                $preM->setCampus($campus);
                $preM->setNome($nome);

            }

        }
        
        $conn->close();
        
        return $preM;
    }
    
    function findAllPredioJSON() {

        $objConn = new Conexao();
        $conn = $objConn->conecta();

        if ($stm = $conn->prepare($this->select)) {

            $stm->execute();

            //atribui o resultado encontrado as variaveis
            $stm->bind_result($id, $campus, $nome);


            //para cada resultado encontrado
            
            $lista = array();
            while ($stm->fetch()) {

                $lista['predios'][] = array(
                    'id' => $id,
                    'campus' => $campus,
                    'nome' => $nome
                );
            }

            $conn->close();
            return $lista;
        }
    }
    
    function findPrediosBycampusJSON($camp) {

        $objConn = new Conexao();
        $conn = $objConn->conecta();

        if ($stm = $conn->prepare($this->select_by_campus)) {
            
            $stm->bind_param("i", $camp);
            $stm->execute();

            //atribui o resultado encontrado as variaveis
            $stm->bind_result($id, $campus, $nome);

            //para cada resultado encontrado
            
            $lista = array();
            while ($stm->fetch()) {
                
                $lista['predios'][] = array(
                    'id' => $id,
                    'campus' => $campus,
                    'nome' => $nome
                );
            }

            $conn->close();
            return $lista;
        }
    }
    
    function findPrediosBycampus($camp) {

        $objConn = new Conexao();
        $conn = $objConn->conecta();
        $preM = new PredioModel();
        $lista = array();
        if ($stm = $conn->prepare($this->select_by_campus)) {
            
            $stm->bind_param("i", $camp);
            $stm->execute();

            //atribui o resultado encontrado as variaveis
            $stm->bind_result($id, $campus, $nome);

            //para cada resultado encontrado
            $i = 0;
            
            while ($stm->fetch()) {
                
                $preM = new PredioModel();
                
                $objCampus = new CampusController();
                
                $campus = $objCampus->selecionarCampusPorId($campus);
                
                $preM->setId($id);
                $preM->setCampus($campus);
                $preM->setNome($nome);
                
                $lista[$i++] = $preM;
            }

            $conn->close();
            return $lista;
        }
    
    }
    
    function existe($predio,$campus) {

        $objConn = new Conexao();
        $conn = $objConn->conecta();

        if ($stm = $conn->prepare($this->select_by_predio_campus)) {
            
            $stm->bind_param('si', $predio,$campus);
            
            $stm->execute();

            while ($stm->fetch()) {
                $conn->close();
                return true;
            }

            $conn->close();
            return false;
        }
    }
}

//$teste = new PredioDAO();

////$dep->setId(3);
//$dep->setDescricao("Administraçao");
//
////$dep = 3;
//if($teste->insert($dep)){
//    printf("Deletado com sucesso!");
//}else{
//    printf("Erro ao deletar!");
//}
//
//var_dump($teste->findAllPredio());

